package com.mininglamp.app.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.mininglamp.app.App;

public class ConfigHelper extends Properties {

	private static final long serialVersionUID = 1L;

	public static final Logger log = LoggerFactory.getLogger(ConfigHelper.class);
	public static final ConfigHelper INSTANCE = new ConfigHelper();

	private ConfigHelper() {
		super();

		try {
			File dir = new File(App.APP_DIR);
			Preconditions.checkState(dir.exists() && dir.isDirectory(),
					"The app.dir is not exists or is not directory.");

			for (File file : dir.listFiles(new FilenameFilter() {
				public boolean accept(File dir, String name) {
					return name.endsWith(".properties");
				}
			})) {
				log.info("Loading property file {}", file.getPath());
				try (FileInputStream fis = new FileInputStream(file)) {
					load(fis);
				}
			}
		} catch (Exception e) {
			log.error("Loading property files failed.", e);
		}
	}

}
